package GUILayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;

import ControlLayer.PersonCtr;
import ModelLayer.Guest;

public class GuestGUI extends JPanel {
	private static final long serialVersionUID = 1L;
	private PersonCtr personCtr;
	private DefaultTableModel guestTableModel;
	private JScrollPane guestTableScrollPane;
	private JPanel showAllGuestPanel;
	private JTable showAllGuestTable;
	private JPanel guestInfoPanel;
	private JPanel choicePanel;

	private JTextField textField_guestId;
	private JTextField txt_Search;
	private JTextField textField_fname;
	private JTextField textField_address;
	private JTextField textField_country;
	private JTextField textField_email;
	private JTextField textField_phone;
	private JTextField textField_lname;
	private JTextField textField_passPort;
	private JTextField textField_discountId;
	private JTextField textField_agencyId;
	private JTextField textField_roomId;
	private JTextField textField_logId;
	private JTextField textField_password;

	private JButton btnSave;
	private JButton btnCreate;

	private JButton btnCancel;
	private JButton btnDelete;

	public GuestGUI() {
		personCtr = new PersonCtr();
		initialize();
	}

	public void initialize() {
		this.setLayout(new BorderLayout());

		showAllGuestPanel = new JPanel();
		this.add(showAllGuestPanel, BorderLayout.CENTER);
		showAllGuestPanel.setBorder(BorderFactory
				.createTitledBorder("Guests Info"));
		showAllGuestPanel.setLayout(new BorderLayout());

		txt_Search = new JTextField();
		txt_Search.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				showAllGuests();
			}
		});
		txt_Search.setColumns(10);

		JPanel searchPanel = new JPanel();
		showAllGuestPanel.add(searchPanel, BorderLayout.NORTH);
		GroupLayout gl_searchPanel = new GroupLayout(searchPanel);
		gl_searchPanel.setHorizontalGroup(gl_searchPanel.createParallelGroup(
				Alignment.LEADING).addComponent(txt_Search));
		gl_searchPanel.setVerticalGroup(gl_searchPanel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_searchPanel
						.createSequentialGroup()
						.addGap(5)
						.addComponent(txt_Search, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)));
		searchPanel.setLayout(gl_searchPanel);

		// show customer info view
		JPanel showGuestPanel = new JPanel();
		this.add(showGuestPanel, BorderLayout.EAST);
		showGuestPanel.setLayout(new BorderLayout());
		showGuestPanel.setBorder(BorderFactory.createTitledBorder("Guest"));

		guestInfoPanel = new JPanel();
		JLabel lblguestId = new JLabel("Guest Id: ");
		JLabel lblfname = new JLabel("First Name: ");
		JLabel lbladdress = new JLabel("Address: ");
		JLabel lblCountry = new JLabel("Country: ");
		JLabel lblPhone = new JLabel("Phone: ");
		JLabel lbllname = new JLabel("Last Name: ");
		JLabel lblPassPort = new JLabel("PassPort Nr: ");
		JLabel lblemail = new JLabel("Email: ");
		JLabel lbldiscount = new JLabel("Discount Id: ");
		JLabel lblroom = new JLabel("Room Id: ");
		JLabel lblagency = new JLabel("Agency Id: ");
		JLabel lbllogId = new JLabel("Log Id: ");
		JLabel lblpassword = new JLabel("Pass Word: ");

		textField_logId = new JTextField();
		textField_password = new JTextField();
		textField_guestId = new JTextField();
		textField_guestId.setEditable(false); // Id is not able changed
		textField_logId.setEditable(false);
		textField_password.setEditable(false);

		textField_fname = new JTextField();
		textField_address = new JTextField();
		textField_country = new JTextField();
		textField_email = new JTextField();
		textField_phone = new JTextField();
		textField_lname = new JTextField();
		textField_passPort = new JTextField();
		textField_discountId = new JTextField();
		textField_agencyId = new JTextField();
		textField_roomId = new JTextField();

		GroupLayout gl = new GroupLayout(guestInfoPanel);
		guestInfoPanel.setLayout(gl);
		guestInfoPanel.setBorder(BorderFactory.createTitledBorder("Info"));
		gl.setAutoCreateGaps(true);
		gl.setAutoCreateContainerGaps(true);

		gl.setHorizontalGroup(gl
				.createSequentialGroup()
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lblguestId)
								.addComponent(lblfname)
								.addComponent(lbladdress)
								.addComponent(lbllname)
								.addComponent(lblPassPort)
								.addComponent(lblPhone)
								.addComponent(lbladdress)
								.addComponent(lblCountry)
								.addComponent(lblPhone).addComponent(lblemail)
								.addComponent(lblroom)
								.addComponent(lbldiscount)
								.addComponent(lblagency).addComponent(lbllogId)
								.addComponent(lblpassword))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(textField_guestId)
								.addComponent(textField_fname)
								.addComponent(textField_lname)
								.addComponent(textField_passPort)
								.addComponent(textField_address)
								.addComponent(textField_country)
								.addComponent(textField_phone)
								.addComponent(textField_email)
								.addComponent(textField_roomId)
								.addComponent(textField_discountId)
								.addComponent(textField_agencyId)
								.addComponent(textField_logId)
								.addComponent(textField_password)));
		gl.setVerticalGroup(gl
				.createSequentialGroup()
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblguestId)
								.addComponent(textField_guestId))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblfname)
								.addComponent(textField_fname))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lbllname)
								.addComponent(textField_lname))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblPassPort)
								.addComponent(textField_passPort))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lbladdress)
								.addComponent(textField_address))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblCountry)
								.addComponent(textField_country))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblPhone)
								.addComponent(textField_phone))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lblemail)
								.addComponent(textField_email))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lblroom)
								.addComponent(textField_roomId))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lbldiscount)
								.addComponent(textField_discountId))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lblagency)
								.addComponent(textField_agencyId))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lbllogId)
								.addComponent(textField_logId))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lblpassword)
								.addComponent(textField_password)));

		showGuestPanel.add(guestInfoPanel, BorderLayout.NORTH);

		choicePanel = new JPanel();
		choicePanel.setLayout(new FlowLayout());
		btnCreate = new JButton("Create");

		btnDelete = new JButton("Delete");
		btnSave = new JButton("Save");
		btnCancel = new JButton("Clear");

		choicePanel.add(btnCreate);

		choicePanel.add(btnSave);
		choicePanel.add(btnDelete);
		choicePanel.add(btnCancel);
		showGuestPanel.add(choicePanel, BorderLayout.CENTER);

		addGuestsTable();
		showAllGuests();

		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!textField_guestId.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Please clear guest fields before create one",
							"Error", JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						int agencyId = 0;
						int discountId = 0;
						if (!textField_agencyId.getText().isEmpty())
							agencyId = Integer.parseInt(textField_agencyId
									.getText());
						if (!textField_discountId.getText().isEmpty())
							discountId = Integer.parseInt(textField_discountId
									.getText());
						personCtr.createGuest(textField_fname.getText(),
								textField_lname.getText(),
								textField_address.getText(),
								textField_country.getText(),
								textField_phone.getText(),
								textField_email.getText(), "Guest",
								textField_passPort.getText(), agencyId,
								discountId);
						JOptionPane.showMessageDialog(null,
								"The guest is created", "Create Guest",
								JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception ex) {
						// TODO number exception
						JOptionPane.showMessageDialog(null,
								"Please input correct values", "Error",
								JOptionPane.ERROR_MESSAGE);
					}
					cleanTextField();
					showAllGuests();
				}
			}
		});

		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (textField_guestId.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Please select a guest", "Error",
							JOptionPane.ERROR_MESSAGE);
				} else {
					int reply = JOptionPane.showConfirmDialog(null,
							"Do you want to delete this guest ?",
							"Delete Guest", JOptionPane.YES_NO_OPTION,
							JOptionPane.WARNING_MESSAGE);
					if (reply == JOptionPane.YES_OPTION) {
						try {
							personCtr.deleteGuest(Integer
									.parseInt(textField_guestId.getText()));
							JOptionPane.showMessageDialog(null,
									"The guest is deleted", "Delete Guest",
									JOptionPane.INFORMATION_MESSAGE);
							cleanTextField();

							showAllGuests();
						} catch (Exception e1) {
						}
					}
				}
			}
		});

		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (textField_guestId.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Please select a guest", "Error",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						int agencyId = 0;
						int discountId = 0;
						int roomId = 0;
						if (!textField_agencyId.getText().isEmpty())
							agencyId = Integer.parseInt(textField_agencyId
									.getText());
						if (!textField_discountId.getText().isEmpty())
							discountId = Integer.parseInt(textField_discountId
									.getText());
						if (!textField_roomId.getText().isEmpty())
							roomId = Integer.parseInt(textField_roomId
									.getText());
						personCtr.updateGuest(
								Integer.parseInt(textField_guestId.getText()),
								textField_fname.getText(),
								textField_lname.getText(),
								textField_address.getText(),
								textField_country.getText(),
								textField_phone.getText(),
								textField_email.getText(), 5,
								textField_passPort.getText(), agencyId,
								discountId, roomId);
						JOptionPane.showMessageDialog(null,
								"The guest info is saved", "Update Guest",
								JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception a) {
						JOptionPane.showMessageDialog(null,
								"Please input correct values", "Error",
								JOptionPane.ERROR_MESSAGE);
					}

					cleanTextField();
					showAllGuests();
				}
			}
		});

		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cleanTextField();
			}
		});

	}

	public void addGuestsTable() {
		guestTableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		guestTableModel.addColumn("ID");
		guestTableModel.addColumn("First Name");
		guestTableModel.addColumn("Last Name");
		guestTableModel.addColumn("PassPort Nr");
		guestTableModel.addColumn("Phone");
		guestTableModel.addColumn("Email");
		guestTableModel.addColumn("Room Id");
		guestTableModel.addColumn("Agency Id");
		guestTableModel.addColumn("Discount Id");

		guestTableScrollPane = new JScrollPane();

		showAllGuestPanel.add(guestTableScrollPane, BorderLayout.CENTER);

		showAllGuestTable = new JTable(guestTableModel);
		showAllGuestTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
		showAllGuestTable.setAutoCreateRowSorter(true);

		showAllGuestTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				int row = showAllGuestTable.rowAtPoint(e.getPoint());
				showGuest(Integer.valueOf((Integer) showAllGuestTable
						.getValueAt(row, 0)));
			}
		});
		guestTableScrollPane.setViewportView(showAllGuestTable);
	}

	public void showAllGuests() {
		guestTableModel.setRowCount(0);
		String searchFilter = txt_Search.getText();

		ArrayList<Guest> guests = personCtr.getAllGuest();

		for (Guest guest : guests) {
			String groupId = "";
			String roomId = "";
			String agencyId = "";
			if (guest.getAgency() != null)
				agencyId = ((Integer) guest.getAgency().getId()).toString();
			if (guest.getCurrentRoom() != null)
				roomId = Integer.toString(guest.getCurrentRoom().getId());
			if (guest.getDiscountGroup() != null)
				groupId = Integer.toString(guest.getDiscountGroup().getId());

			if (((Integer) guest.getID()).toString().toLowerCase()
					.contains(searchFilter.toLowerCase())
					|| guest.getfName().toLowerCase()
							.contains(searchFilter.toLowerCase())
					|| guest.getlName().toLowerCase()
							.contains(searchFilter.toLowerCase())
					|| ((Integer) guest.getAgency().getId()).toString()
							.toLowerCase().contains(searchFilter.toLowerCase())
					|| ((Integer) guest.getCurrentRoom().getId()).toString()
							.toLowerCase().contains(searchFilter.toLowerCase()))
				try {
					guestTableModel.addRow(new Object[] { guest.getID(),
							guest.getfName(), guest.getlName(),
							guest.getPassNumber(), guest.getPhone(),
							guest.getEmail(), roomId, agencyId, groupId });
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e, "Error",
							JOptionPane.ERROR_MESSAGE);
				}
		}

	}

	public boolean showGuest(int guestId) {
		boolean success = true;
		Guest guest = null;
		try {
			if (guestId != 0) {
				guest = (Guest) personCtr.getPerson(guestId);
			}
		} catch (Exception e) {
			success = false;
			JOptionPane.showMessageDialog(null, e, "Error",
					JOptionPane.ERROR_MESSAGE);
			return success;
		}

		if (success) {
			String groupId = "";
			String roomId = "";
			String agencyId = "";
			if (guest.getAgency() != null)
				agencyId = ((Integer) guest.getAgency().getId()).toString();
			if (guest.getCurrentRoom() != null)
				roomId = Integer.toString(guest.getCurrentRoom().getId());
			if (guest.getDiscountGroup() != null)
				groupId = Integer.toString(guest.getDiscountGroup().getId());
			textField_guestId.setText(Integer.toString(guest.getID()));
			textField_fname.setText(guest.getfName());
			textField_lname.setText(guest.getlName());
			textField_passPort.setText(guest.getPassNumber());
			textField_address.setText(guest.getAddress());
			textField_country.setText(guest.getCountry());
			textField_email.setText(guest.getEmail());
			textField_phone.setText(guest.getPhone());
			textField_roomId.setText(roomId);
			textField_agencyId.setText(agencyId);
			textField_discountId.setText(groupId);
			textField_logId.setText(guest.getLogId());
			textField_password.setText(guest.getPassword());

		}
		return success;
	}

	public void refreshTable() {

	}

	public int getTextFieldId(JTextField textField) {
		int id = 0;
		try {
			id = Integer.parseInt(textField.getText());
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null, "Please input number", "Error",
					JOptionPane.ERROR_MESSAGE);
		} finally {
			textField.setText("");
		}
		return id;
	}

	public void cleanTextField() {
		txt_Search.setText("");
		textField_guestId.setText("");
		textField_fname.setText("");
		textField_lname.setText("");
		textField_address.setText("");
		textField_country.setText("");
		textField_email.setText("");
		textField_phone.setText("");
		textField_discountId.setText("");
		textField_passPort.setText("");
		textField_roomId.setText("");
		textField_agencyId.setText("");
		textField_logId.setText("");
		textField_password.setText("");
	}

}
